package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * packageName com.sky.service.impl
 *
 * @author fenglai
 * @version JDK 11
 * @className CategoryServiceImpl
 * @date 2023/12/10
 * @description TODO
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;


    @Override
    public List<Category> selectCategoryByType(Integer type) {

        return categoryMapper.getCategory(Category.builder().type(type).build());

    }

    @Override
    public PageResult pageSelectCategory(CategoryPageQueryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto, category);
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Category> category1 = categoryMapper.getCategory(category);
        Page p = (Page) category1;
        return new PageResult(p.getTotal(), p.getResult());

    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.addCategory(category);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);

        categoryMapper.updateCategory(category);

    }

    @Override
    public void updateCategoryStatus(Integer status, Long id) {

        categoryMapper.updateCategory(Category.builder().status(status).id(id).build());

    }

    @Override
    public void deleteCategory(Long id) {
        List<Setmeal> setmeal = setmealMapper.getSetmeal(Setmeal.builder().categoryId(id).build());
        List<Dish> dish = dishMapper.getDish(Dish.builder().categoryId(id).build());
        List<Category> category = categoryMapper.getCategory(Category.builder().id(id).build());
        if(category.get(0).getStatus()==StatusConstant.ENABLE){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_SALE);
        }
        if(setmeal.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        if(dish.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        categoryMapper.deleteCategory(id);
    }
}